﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using A2DFramework.InstanceService;

namespace A2DFramework.LockService
{
    public class LockService:ILock
    {
        private ILockProvider lockProvider = ObjectFactory.Resolve<ILockProvider>();

        public A2DLock AcquireLock(string lockName)
        {
            return AcquireLock(lockName, TimeSpan.Zero);
        }

        public A2DLock AcquireLock(string lockName, TimeSpan timeout)
        {
            string innerLockName = string.Format("{0}.{1}", Configuration.LockServiceConfiguration.RedisSettings.LockPrefix, lockName);

            A2DLock a2dlock = new A2DLock(this);

            a2dlock.LockName = lockName;
            a2dlock.RealLockIdentity = innerLockName;

            a2dlock.RawLockObject=lockProvider.AcquireLock(innerLockName, timeout);

            return a2dlock;
        }

        public void ReleaseLock(A2DLock lockObj)
        {
            if (lockObj!=null)
                lockProvider.ReleaseLock(lockObj.RawLockObject);
        }
    }
}
